<?php
/***************
* @Project	Warabi
* @Author	Feifei
* @Date		04/05/2009
***************/

/**this module is to handler the database issues**/


class wbDatabase{
	/**database handler**/
	var $dbhandler;

	function __get($att){
		return $att;
	}

	function __set($att , $val){
		$this->$att = $val;
	}

	function __call($method , $p){
		if($method)
			if(is_object($p[0]))
				$this->displayObject($p[0]);
			else if(is_array($p[0]))
				$this->displayArray($p[0]);
			else
				$this->displayScalar($p[0]);
	}

	function wbDatabase(){
		global $wbDBserver , $wbDBname , $wbDBuser , $wbDBpassword , $wbDBport ;

		$this->dbhandler = new mysqli($wbDBserver , $wbDBuser , $wbDBpassword , $wbDBname , $wbDBport);
		if (mysqli_connect_errno()){
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit;
		}
	}

	function query($query){
		if(!$query){
			echo "Please check the sql query";
			exit;
		}
		$result = $this->dbhandler->query($query);
		return $result;
	}
	
	
	/**@ $type:MYSQLI_NUM | MYSQLI_ASSOC | MYSQLI_BOTH**/
	/**return value as array for allresult
	 MYSQLI_NUM : $row[0], $row[1];  
	 MYSQLI_ASSOC : $row["Name"], $row["CountryCode"];  
	 MYSQLI_BOTH :  $row[0], $row["CountryCode"];
	**/
	function fetchResultBySql($query,$type=MYSQL_NUM){
		$resultset = $this->query($query);
		if($this->countResult($resultset)!=0){
			$result = $resultset->fetch_array($type);
			$resultset->close();
		}
		return $result;
	}

	/**@ $type:MYSQLI_NUM | MYSQLI_ASSOC | MYSQLI_BOTH**/
	/**return value as array for allresult
	 MYSQLI_NUM : $row[0], $row[1];  
	 MYSQLI_ASSOC : $row["Name"], $row["CountryCode"];  
	 MYSQLI_BOTH :  $row[0], $row["CountryCode"];
	**/
	function fetchResultBySet($result , $type=MYSQL_NUM){
		if($type&&($this->countResult($result)!=0)){	
			return $result->fetch_array($type);
		}
	}


	/***return field number**/
	function countFields($result){
		if($result)	return $result->field_count;
	}
	
	/***return result number**/
	function countResult($result){
		return $result->num_rows;
	}


	function getCharset(){
		return $this->dbhandler->character_set_name();
	}
	
	function freeResult($result){
		if($result)	$result->free();
	}

	function disconnect(){
		$this->dbhandler->close();
	}

}

?>